package com.zzyweb.managementsystem.service.impl;

import com.zzyweb.managementsystem.mapper.MenuMapper;
import com.zzyweb.managementsystem.pojo.Menu;
import com.zzyweb.managementsystem.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhiyi
 * @date 9/23/25
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getMenuByUsername(String username) {
        List<Menu> menuList = menuMapper.selectMenusByUserName(username);
        List<Menu> menuLevel1 = new ArrayList<>();
        for (Menu m: menuList) {
            if (m.getPid().equals(0)) {
                menuLevel1.add(m);
            }
        }
        for (Menu m1:menuLevel1) {
            for (Menu m: menuList) {
                if (m.getPid().equals(m1.getMid())) {
                    if (m1.getChildren() == null) {
                        m1.setChildren(new ArrayList<>());
                    }
                    m1.getChildren().add(m);
                }
            }
        }
        System.out.println(menuLevel1);
        return menuLevel1;
    }
}
